__author__ = 'zoulida'

#from sklearn.datasets.samples_generator import make_blobs
from sklearn.datasets import make_blobs, make_moons
# make_blobs聚类数据生成器


x,y_true = make_blobs(n_samples = 3000,   # 生成300条数据
                      centers = 9,        # 四类数据
                      cluster_std = 0.5,  #[0.9,1.0,1,0.8],  # 方差一致
                      random_state = 35)
print(x[:5])
print(y_true[:5])
# n_samples → 待生成的样本的总数。
# n_features → 每个样本的特征数。
# centers → 类别数
# cluster_std → 每个类别的方差，如多类数据不同方差，可设置为[1.0,3.0]（这里针对2类数据）方差越小越集中
# random_state → 随机数种子
# x → 生成数据值， y → 生成数据对应的类别标签


from matplotlib import pyplot as plt
plt.scatter(x[:,0],x[:,1],s = 10,alpha = 0.8, c = y_true)
plt.grid()
plt.show()
# 绘制图表

plt.subplot(122)
x1, y1 = make_moons(n_samples=1000, noise=0.1)
plt.title('make_moons function example')
plt.scatter(x1[:, 0], x1[:, 1], marker='o', c=y1)
plt.show()


from sklearn.cluster import KMeans
# 导入模块

kmeans = KMeans(n_clusters = 4)  # 这里为4簇
kmeans.fit(x)
y_kmeans = kmeans.predict(x)
centroids = kmeans.cluster_centers_
# 构建模型，并预测出样本的类别y_kmeans
# kmeans.cluster_centers_：得到不同簇的中心点

plt.scatter(x[:,0],x[:,1],c = y_kmeans, cmap = 'Dark2', s= 50,alpha = 0.5,marker='x')
plt.scatter(centroids[:,0],centroids[:,1],c = [0,1,2,3], cmap = 'Dark2',s= 70,marker='o')
plt.title('K-means 300 points\n')
plt.xlabel('Value1')
plt.ylabel('Value2')
plt.grid()
# 绘制图表
plt.show()

